<template>
  <div class="product">
    <!-- 按钮 -->
    <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">
        新增
      </el-button>
    </div>
    <!-- /按钮 -->
    <!-- 表格 -->
    <el-table size="small" :data="products">
      <el-table-column label="编号" type="index" :index="1" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="单价" prop="price" />
      <el-table-column label="介绍" prop="introduce" />
      <el-table-column label="所属分类" prop="category.name" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
          <el-button type="primary" size="mini" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格 -->
    <!-- 模态框 -->
    <el-dialog title="录入产品信息" :visible.sync="visible" width="50%">
      {{ form }}
      <el-form label-width="80px">
        <el-form-item label="产品名称">
          <el-input v-model="form.name" placeholder="" />
        </el-form-item>
        <el-form-item label="所属栏目">
          <!-- <el-select v-model="form.categoryId">
            <el-option v-for="c in categories" :key="c.id" :label="c.name" :value="c.id" />
          </el-select> -->
          <el-cascader
            :v-model="form.categoryId"
            :options="categories"
            :props="{ label:'name',value:'id', checkStrictly: true ,emitPath:false}"
            clearable
          />
        </el-form-item>
        <el-form-item label="产品单价">
          <el-input v-model="form.price" placeholder="" />
        </el-form-item>
        <el-form-item label="产品介绍">
          <el-input v-model="form.introduce" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      name: '产品管理',
      visible: false,
      products: [],
      form: {},
      categories: []
    }
  },
  created() {
    // 调用加载数据方法进行加载
    this.loadProducts()
    // 调用查询栏目的方法加载栏目数据
    this.loadCategories()
  },
  methods: {
    toAdd() {
      this.form = {}
      this.visible = true
    },
    // 修改
    edit(row) {
      this.form = row
      this.visible = true
    },
    del(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除
        const url = 'http://120.77.67.62:8888/product/deleteById'
        post(url, { id: row.id }).then((response) => {
          // 提示
          this.$message({ type: 'success', message: response.message })
          // 刷新数据
          this.loadProducts()
        })
      })
    },
    // 加载产品数据
    loadProducts() {
      const url = 'http://120.77.67.62:8888/product/findAllWithCategory'
      get(url).then((response) => {
        this.products = response.data
      })
    },
    // 加载栏目数据
    loadCategories() {
      const url = 'http://120.77.67.62:8888/category/selectCategoryTree'
      get(url).then((response) => {
        this.categories = response.data
      })
    },
    // 提交
    submit() {
      const url = 'http://120.77.67.62:8888/product/saveOrUpdate'
      post(url, this.form).then((response) => {
        // 提示
        this.$message({ type: 'success', message: response.message })
        // 关闭模态框
        this.visible = false
        // 刷新数据
        this.loadProducts()
      })
    }
  }
}
</script>
